using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BHXH.Core.Domain;
using BHXH.Core.IService;
using FX.Data;
using NHibernate;
using NHibernate.Cfg;
using System.Reflection;

namespace BHXH.Core.ServiceImp
{
    public class hs_01DKBB_msService : BaseService<hs_01DKBB_ms, int>, Ihs_01DKBB_msService
    {
        static ISession mySession;
        public hs_01DKBB_ms tt_dk_hs_bh;
        public hs_01DKBB_msService(string sessionFactoryConfigPath)
            : base(sessionFactoryConfigPath)
        { }

        public hs_01DKBB_ms Load_DangKi_BaoHiem(int ID, String IDref)
        {
            using (mySession = this.NHibernateSession.SessionFactory.OpenSession())
            {
                tt_dk_hs_bh = new hs_01DKBB_ms();

                IList<hs_01DKBB_ms> query = mySession.CreateQuery("FROM hs_01DKBB_ms as tt_dk_hs_bh where tt_dk_hs_bh.IDRef = :IDRef ")
                    .SetParameter("IDRef", IDref)
                    .List<hs_01DKBB_ms>();

                if (query.Count > 0)
                {
                    tt_dk_hs_bh = query[0];
                    return tt_dk_hs_bh;
                }
                else
                {
                    //chua chua dang ki, lay ra tu bang khac va insert 
                    IList<thongtincongty> query_thongtincongty = mySession.CreateQuery("FROM thongtincongty as ttct where ttct.ID = :id ")
                    .SetParameter("id", ID)
                    .List<thongtincongty>();
                    thongtincongty tthsdkbh = new thongtincongty();

                    tthsdkbh = query_thongtincongty[0];
                    tt_dk_hs_bh.KinhGui = tthsdkbh.QuanHuyen;
                    tt_dk_hs_bh.TenDV = tthsdkbh.TenCongTy;
                    tt_dk_hs_bh.DiaChi = tthsdkbh.DiaChi;
                    tt_dk_hs_bh.DienThoai = tthsdkbh.DienThoai;
                    tt_dk_hs_bh.Email = tthsdkbh.Email;
                    tt_dk_hs_bh.LHDV = tthsdkbh.MaLHinhDN;
                    tt_dk_hs_bh.MST = tthsdkbh.MaSoThue;

                    return tt_dk_hs_bh;
                }
            }
        }

        public void UpDate_DangKi_Bao_Hiem(hs_01DKBB_ms ttdk)
        {
            using (mySession = this.NHibernateSession.SessionFactory.OpenSession())
            {                
                mySession.Update(ttdk);
                mySession.Flush();                
            }
        }

        public void Add_DangKi_Bao_Hiem(hs_01DKBB_ms ttdk)
        {
            using (mySession = this.NHibernateSession.SessionFactory.OpenSession())
            {
                CreateNew(ttdk);
                CommitChanges();                
            }
        }
    }

}